var C1DAY = 86400000;

/*
 * 月の開始を返す
 */
function getFirstdayOfMonth(cur_date) {
	var time = Date.parse(cur_date);			// YYYY/MM/DD
	var cur_date = new Date();
	cur_date.setTime(time);
	var start_date;

	var year = cur_date.getYear();							// 年
	if(year < 2000) { year += 1900; }
	var month = cur_date.getMonth() + 1;					// 月
	if(month < 10)  { month = "0" + month; }

	start_date = year + "/" + month + "/01";

	return start_date;
}

/*
 * 月の終了を返す
 */
function getLastdayOfMonth(cur_date) {
	var time = Date.parse(cur_date);			// YYYY/MM/DD
	var cur_date = new Date();
	cur_date.setTime(time);
	var end_date;

	var year = cur_date.getYear();							// 年
	if(year < 2000) { year += 1900; }
	var month = cur_date.getMonth() + 1;					// 月
	if(year < 2000) { year += 1900; }
	if(month == 12 || month == 10) {
		end_date = year + "/" + month + "/31";
	} else if(month == 11) {
		end_date = year + "/" + month + "/30";
	} else if(month == 2) {
		if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
			end_date = year + "/0" + month + "/29";
		} else {
			end_date = year + "/0" + month + "/28";
		}
	} else if(month == 4 || month == 6 || month == 9) {
		end_date = year + "/0" + month + "/30";
	} else {
		end_date = year + "/0" + month + "/31";
	}

	return end_date;
}

/*
 * 週の開始を返す
 */
function getFirstdayOfWeek(cur_date) {
	var time = Date.parse(cur_date);			// YYYY/MM/DD
	var cur_date = new Date();
	cur_date.setTime(time);
	var start_date;

	var w = cur_date.getDay();
	var work_date = new Date();
	work_date.setTime(time - (w * C1DAY));
	var year = work_date.getYear();							// 年
	if(year < 2000) { year += 1900; }
	var month = work_date.getMonth() + 1;					// 月
	if(month < 10)  { month = "0" + month; }
	var day = work_date.getDate();							// 日
	if(day < 10)    { day = "0" + day; }

	start_date = year + "/" + month + "/" + day;

	return start_date;
}

/*
 * 週の終了を返す
 */
function getLastdayOfWeek(cur_date) {
	var time = Date.parse(cur_date);			// YYYY/MM/DD
	var cur_date = new Date();
	cur_date.setTime(time);
	var end_date;

	var w = cur_date.getDay();
	var work_date = new Date();
	work_date.setTime(time + ((6 - w) * C1DAY));
	var year = work_date.getYear();							// 年
	if(year < 2000) { year += 1900; }
	var month = work_date.getMonth() + 1;					// 月
	if(month < 10)  { month = "0" + month; }
	var day = work_date.getDate();							// 日
	if(day < 10)    { day = "0" + day; }

	end_date = year + "/" + month + "/" + day;

	return end_date;
}
/*
 * 入力値が正しい日付文字列かどうかチェックする。
 *
 * indate :チェックする日付け
 * imMsg  :エラーメッセージに追加する
 *
 *
 * 帰り値 YYYYMMDD：正常   "":異常
 *
 */
function dateStringCheck(inDate, inMsg){

	var retval ="";
	//未入力はチェックしない
	if( inDate == "" ) return retval;
	var werror = false;
	var errMsg = inMsg + "日付の指定に誤りがあります。";
	var stp = inDate.indexOf("/");
	var enp = inDate.lastIndexOf("/");

	if( stp == 0 ) werror = true;
	if( enp == 0 ) werror = true;
	if( stp == enp )  werror = true;

	if ( werror == true ){
		alert( errMsg);
		return retval;
	}

    var ys = inDate.substr(0,stp);
    var ms = inDate.substr(stp+1,enp-stp-1);
    var ds = inDate.substr(enp+1,inDate.length);
    if( ys=="" && ms=="" && ds=="" ){
		alert( errMsg);
		return retval;
    }else if( ys.match(/\D/) || ms.match(/\D/) || ds.match(/\D/) ){
		alert(errMsg);
		return retval;
    }
    // 数値に変換
    var yi = parseInt(ys,10);
    var mi = parseInt(ms,10);
    var di = parseInt(ds,10);
    // 日付の妥当性チェック
    if( yi>=1900 && yi<=9999 && mi>=1 && mi<=12 && di>=1 && di<=31 ){
        var datei = new Date(yi,mi-1,di);
        if( datei.getFullYear() != yi ){
    		alert(errMsg);
    		return retval;
        }
        if( datei.getMonth() != mi-1 ){
    		alert(errMsg);
    		return retval;
        }
        if( datei.getDate() != di ){
    		alert(errMsg);
    		return retval;
        }
    }else{
		alert( errMsg);
		return retval;
    }
    ys = yi.toString();
    ms = ( mi<10 ? "0"+mi.toString() : mi.toString() );
    ds = ( di<10 ? "0"+di.toString() : di.toString() );


    return ys+ms+ds;
}
